package primary.tree;

/**
 * @Author Elephas
 * @Date 2022/1/10
 **/
public class SortedArrayToBST {

    /*
        思路:二分查找可以构建一个二叉搜索树。分治法
     */
    public TreeNode sortedArrayToBST(int[] nums){
        return sortedArrayToBSTHelper(nums,0,nums.length-1);
    }

    public TreeNode sortedArrayToBSTHelper(int[] nums,int low,int high ){
        int mid = low + (high-low)/2;
        if(low > high){
            return null;
        }
        TreeNode root = new TreeNode(nums[mid],null,null);
        root.left = sortedArrayToBSTHelper(nums,low,mid-1);
        root.right = sortedArrayToBSTHelper(nums, mid+1,high);
        return root;
    }
}
